package com.serkol.component;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.service.LoginLogService;
import com.serkol.constant.AuthConstant;
import com.serkol.utils.sm.SM4Utils;
import org.apache.commons.lang3.StringUtils;


public class TokenUtils {

    private LoginLogService logService;

    public TokenUtils(LoginLogService logService) {
        this.logService = logService;
    }


    public boolean isToken(String token, String ipAddr) {
        boolean result = false;
        LoginLog login = logService.isLogin(token);
        System.out.println(login);
        if (login != null) {
            System.out.println("------:" + ipAddr);
            String ip = login.getIp();
            AuthConstant.setLoginLog(null);
            if (login.getState() == 0) {
                if (StringUtils.isNotEmpty(ip)) {
                    result = ip.equals(ipAddr);
                    AuthConstant.setLoginLog(login);
                } else {
                    result = false;
                }
            } else {
                result = false;
            }


        }
        return result;

    }

    public boolean isToken(String token) {
        boolean result = false;
        LoginLog login = logService.isLogin(token);
        AuthConstant.setLoginLog(null);
        if (login != null) {

            if (login.getState() == 0) {
                result = true;
                AuthConstant.setLoginLog(login);
            } else {
                result = false;

            }
        }
        return result;
    }

    public LoginLog getToken(String token) {
        LoginLog login = logService.isLogin(token);
        if (login != null) {
            if (login.getState() == 0) {
                return login;
            } else {
                return null;
            }
        } else {
            return null;
        }

        // return login;
    }


    public LoginLog getToken(long userId) {

        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LoginLog::getUserId, userId).eq(LoginLog::getState, 0);
        LoginLog login = logService.getOne(queryWrapper);
        if (login != null) {
            if (login.getState() == 0) {
                return login;
            } else {
                return null;
            }
        } else {
            return null;
        }

        // return login;
    }


    public String getToken(LoginLog adminToken) {
        SM4Utils sm4Utils = new SM4Utils();
        adminToken.setToken("");
        String s = JSONObject.toJSONString(adminToken);

        String encode1 = sm4Utils.encryptData_ECB(s);
       // String encode1 = Base64.encodeBase64String(s.getBytes());
        //   String encode1 = URLEncoder.encode(encode, "utf-8");
        System.out.println(encode1);
        adminToken.setToken(encode1);
        System.out.println(adminToken);
        LambdaUpdateWrapper<LoginLog> userQueryWrapper = Wrappers.<LoginLog>lambdaUpdate()
                .eq(LoginLog::getUserId, adminToken.getUserId())
                .eq(LoginLog::getState, 0);
        LoginLog user = new LoginLog();
        user.setState(1);
        long count = logService.count(userQueryWrapper);
        if (count > 0) {
            boolean update = logService.update(user, userQueryWrapper);
            if (update) {
                boolean save = logService.save(adminToken);
                if (save) {
                    return encode1;
                } else {
                    return "";
                }
            } else {
                return "";
            }
        } else {
            boolean save = logService.save(adminToken);
            if (save) {
                return encode1;

            } else {
                return "";
            }
        }


    }

    public LoginLog getAdmin(String token) {
        LoginLog token1 = getToken(token);
        System.out.println(token1);
        if (token1 != null) {
            if (token1.getState() == 0) {
                return token1;
            } else {
                return null;
            }


        } else {
            return null;
        }


    }

    public long getAdminId(String token) {


        if (isToken(token)) {
            SM4Utils sm4Utils = new SM4Utils();
            String s = sm4Utils.decryptData_ECB(token);
            //  String encode1 = URLDecoder.decode(token, "utf-8");
           // String encode = new String(Base64.decodeBase64(token).toString());
         //   JSONObject object = JSONObject.parseObject(s);
            LoginLog t = JSONObject.parseObject(s,LoginLog.class);

            System.out.println("-------" + s);
            long user_id = t.getUserId();
            return user_id;
        } else {
            return 0;
        }


    }


}
